﻿/* =============================================
 * 名    称：MessageHandler
 * 功能描述：MessageHandler
 * 作    者：yanwei.yang
 * 添加时间：2015/4/15 17:39:03
 * =============================================*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Summer.WeiXin.Core
{
    public class MessageHandler
    {

        /// <summary>
        /// 发送文本消息
        /// </summary>
        /// <param name="me"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public string sendTextMessage(Message me, string content)
        {
            string temp = @"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName>
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[{3}]]></Content>
</xml>";
            temp = string.Format(temp, me.FromUserName, me.ToUserName, Message.GetNowTime(), content);
            return temp;
        }


        /// <summary>
        /// 发送图文消息
        /// </summary>
        /// <param name="me"></param>
        /// <param name="media_id"></param>
        /// <returns></returns>
        public string sendImageMessage(Message me, string media_id)
        {
            string temp = @"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName>
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<Image>
<MediaId><![CDATA[{3}]]></MediaId>
</Image>
</xml>";
            temp = string.Format(temp, me.FromUserName, me.ToUserName, Message.GetNowTime(), media_id);
            return temp;

        }

        /// <summary>
        /// 发送图文消息
        /// </summary>
        /// <param name="me"></param>
        /// <param name="newslist"></param>
        /// <returns></returns>

        public string sendNewsMessage(Message me, List<News> newslist)
        {
            StringBuilder sb = new StringBuilder();
            if (newslist != null && newslist.Count > 0)
            {
                int count = newslist.Count;
                sb.Append("<ArticleCount>" + count + "</ArticleCount>");
                sb.Append("<Articles>");
                for (int i = 0; i < newslist.Count; i++)
                {
                    var cell = newslist[i];
                    sb.Append("<item>");
                    sb.Append("<Title><![CDATA[" + cell.Title + "]]></Title> ");
                    sb.Append("<Description><![CDATA[" + cell.Description + "]]></Description>");
                    sb.Append("<PicUrl><![CDATA[" + cell.PicUrl + "]]></PicUrl>");
                    sb.Append("<Url><![CDATA[" + cell.Url + "]]></Url>");
                    sb.Append("</item>");
                }
                sb.Append("<Articles>");
                sb.Append("</Articles>");
            }
            string temp = @"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[news]]></MsgType>" + sb.ToString() + "</xml> ";
            temp = string.Format(temp, me.FromUserName, me.ToUserName, Message.GetNowTime());
            return temp;
        }
    }
}
